/**
 * @HelloWorldAction
 * 
 * @title HelloWorldAction

 */

package controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.Model;

public class HelloWorldAction extends Action {
    

    public HelloWorldAction(Model model) {

        //userDAO = model.getUserDAO();
    }

    public String getName() {
        return "helloWorld.do";
    }

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
        String originPage = "HelloWorld.jsp";
        String successPage = "HelloWorld.jsp";
        
        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        
        //get all the parameter from the form
        String requestType = request.getParameter("Submit");
        String nameString = request.getParameter("name");
        
        //reload the form field
        request.setAttribute("name", nameString);
 
        
        //check if user has been logged in
        //check the session
        
        //validate if user has necessary qualification
        //check the session
        
        // check for get or post request
        //if there is no parameter get passed, return to origin page
        if (requestType == null) {
            messages.add("You are now seeing HelloWorldAction");
            return originPage;     
        }
        
        //validate the input
        if (nameString != null && nameString.length() < 3) {
            errors.add("Name must be more than three characters");
            return originPage;
        }
        
        

        // at this stage, all validation should have been done
        try {
            
            if (nameString != null){
                messages.add("Hi " +nameString + "! You are now seeing HelloWorldAction" );   
            } else {
                messages.add("You are now seeing HelloWorldAction. Please input the name" );
            }
            
            return successPage;

        } catch (Exception e) {
            errors.add("Something is not right");
            return originPage;
        }

    }
}
